require("public")
init("",0)
width,height = getScreenSize()
ret,results = showUI('ui.json')

if ret == 0 then
	lua_exit()	
end

if width/height == 750/1334 then
	setScreenScale(750,1334)    --在540*960分辨率的手机中开发了脚本，要在720*1280的设备中运行 -
else
	showHUD(id,"分辨率比例不同，不保证脚本完美运行",11,"0xffff0000","0xffffffff",0,100,0,228,32)      --显示HUD内容
	setScreenScale(750,1334)    --在540*960分辨率的手机中开发了脚本，要在720*1280的设备中运行 -
	
end

function Split(s, sp)  
	 res = {}  
	 temp = s  
	 len = 0  
	while true do  
		len = string.find(temp, sp)  
		if len ~= nil then  
			 result = string.sub(temp, 1, len-1)  
			temp = string.sub(temp, len+1)  
			table.insert(res, result)  
		else  
			table.insert(res, temp)  
			break  
		end  
	end  
	
	return res  
end  

res = Split(results["CheckBoxGroup1"],"@")
task = {}
for v,s in pairs(res) do
	--[[
	if s=="0" then
		mycoin = true
	elseif s=="1" then
		stealcoin = true;
	elseif s=="2" then
		decorate = true;
		
	elseif s=="3" then
		lottery = true;
		
	elseif s=="4" then
		email = true;
		
	end
	--]]  
	if s ~= "" then
		table.insert(task,s);
	end
	
end
id = createHUD()     --创建一个HUD

if #task == 0 then
	showHUD(id,"未选择任何任务，脚本自动退出",28,"0xffff0000","0xffffffff",0,width-500,0,500,32)      --显示HUD内容
	mSleep(3000)
	
	lua_exit();
end
C_TASK = nil
function nextTask()
	 findNextTask = false;
	for v,s in pairs(task) do 
		if C_TASK == nil then
			C_TASK = s
			break;
		end
		
		
		if findNextTask then
			C_TASK = s
			break;
		end
		
		if s == C_TASK then
			findNextTask = true;
		end
	end
	
end

showHUD(id,"分辨率为"..width..","..height,28,"0xffff0000","0xffffffff",0,width-300,0,300,32)      --显示HUD内容


if width/height == 750/1334 then
	setScreenScale(750,1334)    --在540*960分辨率的手机中开发了脚本，要在720*1280的设备中运行 -
else
	showHUD(id,"分辨率比例不同，不保证脚本完美运行",11,"0xffff0000","0xffffffff",0,100,0,228,32)      --显示HUD内容
end



function FD_FIND_TZ(tab)
	if tab == nil then
		return
	end
	
	for v,s in pairs(tab) do
		x, y = findColor({s[1], s[2], s[3], s[4]},s[5],95, 0, 0, 0)
		if x > -1 then
			return {x,y}
		end
	end
	
	return nil
end




function FD_C_UI()
	for name, value in pairs(FD_MAIN_UI) do
		x, y = findColor({value[1], value[2], value[3], value[4]},value[5],95,0,0,0)
		if x > -1 then
			return name;
		end
	end
	
	return nil
end

function FD_F_COLOR(tab)
	if tab == nil then
		return
	end
	
	x, y = findColor({tab[1], tab[2], tab[3], tab[4]},tab[5],93, 0, 0, 0)
	if x > -1 then
		return {x,y}
	end
	
	return nil
end

function FD_F_IMG(image)
	x, y = findImageInRegionFuzzy(image, 60, 0, 0, width, height, 0);
	if x ~= -1 and y ~= -1 then        --如果在指定区域找到某图片符合条件
		return {x,y}
	else                               --如果找不到符合条件的图片
		--sysLog("未找到符合条件的坐标！");
		return nil
	end
end

function FD_F_GO()
	point = findColors({0, 0, 749, 1333}, 
		"0|0|0x96ecfd,-1|0|0xadecf2,-1|1|0x89cffe,0|2|0x6f8eff,-2|2|0x7eb4ff",
		95, 0, 0, 0)
	if #point ~= 0 then
		return true
	else 
		return false
	end
end

function FD_Q_TAP(tab)
	if tab == nil then
		return
	end
	shou = createHUD()     --创建一个HUD
	showHUD(shou,"",12,"0x00ff0000","finger.png",0,tab[1],tab[2],40,48)     --变更显示的HUD内容
	
	
	touchDown(1, tab[1], tab[2])
	mSleep(50)
	touchMove(1, tab[1], tab[2])
	mSleep(50)
	touchUp(1, tab[1], tab[2])  
	
	hideHUD(shou)
end

repeatCount = 0;
taskEnd = false;
while true do
	mSleep(500)
	
	C_UI = FD_C_UI() --当前窗口
	
	if C_UI ~= nil then
		showHUD(id, C_UI,28,"0xffff0000","0xffffffff",0,width-300,0,300,32)      --显示HUD内容
	else
		showHUD(id,"",28,"0xffff0000","0xffffffff",0,width-300,0,300,32)      --显示HUD内容
	end
	
	if C_TASK == nil then
		nextTask();
	end
	
	--收自己家金币
	if C_TASK == "0" then
		GO_HOME = FD_F_COLOR(FD_FIND_POINT.别人家.回家)
		if GO_HOME ~= nil then
			FD_Q_TAP(GO_HOME)
			mSleep(2000);
		else
			if C_UI=="自己家" then
				 TAP_POINT = FD_FIND_TZ(FD_TEZHENG_POINT.主窗口.金币)
				if TAP_POINT ~= nil then
					FD_Q_TAP(TAP_POINT)
				else 
					nextTask()
				end
			end
		end
	elseif C_TASK == "1" then
		 TAP_POINT = FD_FIND_TZ(FD_TEZHENG_POINT.主窗口.金币)
		if TAP_POINT ~= nil then
			FD_Q_TAP(TAP_POINT)
		else 
			
			--如果出现邀请代码已经偷完了
			
			local nomore = FD_F_COLOR(FD_FIND_POINT.别人家.没有更多)
			
			point = findColors({0, 1114, 749, 1333}, 
				"0|0|0xd88e3c,0|2|0xe9b438,-4|8|0xffce0f,-3|10|0xfbe253,-3|18|0xdfa436",
				95, 0, 0, 0)
			
			
			
			local moshengren = FD_F_COLOR(FD_FIND_POINT.别人家.放大陌生人)
			if #point < 5 and moshengren ~= nil then   --如果该点的颜色值等于0xffffff
				nextTask()
			end
			
			YAOQING = FD_F_COLOR(FD_FIND_POINT.别人家.邀请)
			if YAOQING ~= nil then
				 MOSHENGREN = FD_F_COLOR(FD_FIND_POINT.别人家.陌生人)
				FD_Q_TAP(MOSHENGREN)				
			else 
				
				GO_STEAL = FD_F_COLOR(FD_FIND_POINT.自己家.可以偷)
				if GO_STEAL ~= nil then
					GO_STEAL = {GO_STEAL[1]-30,GO_STEAL[2]+30}
					FD_Q_TAP(GO_STEAL)
				else 
					touchDown(1, width/2, height - 50)
					mSleep(300)
					touchMove(1, width/2 - 200, height - 50)
					mSleep(500)
					touchUp(1, 150, 150)  
					mSleep(1000)
					
				end
			end
			
			
			
		end
		
		
	elseif C_TASK == "2" then
		--如果在别人家，先回家
		GOUMAI = FD_FIND_TZ(FD_TEZHENG_POINT.主窗口.购买)
		GUANBI = FD_F_COLOR(FD_FIND_POINT.自己家.关闭)
		if GOUMAI ~= nil and GUANBI ~= nil then
			GOUMAI = {GOUMAI[1],GOUMAI[2]-50}
			FD_Q_TAP(GOUMAI)
		else
			
			 TAP_POINT = FD_FIND_TZ(FD_TEZHENG_POINT.主窗口.金币)
			if TAP_POINT ~= nil then
				FD_Q_TAP(TAP_POINT)
			else 
				GO_HOME = FD_F_COLOR(FD_FIND_POINT.别人家.回家)
				if GO_HOME ~= nil then
					FD_Q_TAP(GO_HOME)
					mSleep(2000);
				else
					ZHUANGBAN = FD_F_COLOR(FD_FIND_POINT.自己家.装扮)
					FD_Q_TAP(ZHUANGBAN)
					
					CHOUJIANG = FD_F_COLOR(FD_FIND_POINT.自己家.抽奖)
					FD_Q_TAP(CHOUJIANG)
					
					XUANZETA = FD_F_COLOR(FD_FIND_POINT.自己家.选择他)
					FD_Q_TAP(XUANZETA)
					
				end
			end	
		end
		
		
	end
	
	for v,s in pairs(FD_FIND_POINT.杂七杂八) do
		TAP_POINT = FD_F_COLOR(s)
		FD_Q_TAP(TAP_POINT)
	end
	
end

